import TableData from "@/components/public/el-table-pagination"; //主页面列表
import TableLyout from '@/components/commonPublic/tableLyoutDetail.vue'
import CustomDialog from "@/components/PhysicalQualityPage/rebarThicknessManage/rebarDetectionTask/index.vue";

import formJs from "./regexDetail.js"; //新增弹框配置
import {
    getSteelProtectionTestTaskLists, //列表
    copySteelProtectionTestTask,// 复制
    submitSteelProtectionTestTaskByAuthor, //提交
    deleteSteelProtectionTestTask,//删除
} from "@/api/request.js";
export default {
    name: "rebarDetectionTaskText",
    data() {
        return {
            name: "rebarDetectionTaskText",
            radio: "待办",
            treelength: true, //左侧树显示隐藏
            btnObj: [], //按钮
            loading: false,
            getList: {
                // projectId: "",// 项目ID
                fCompanyid: "",// 标段ID
                startDates: "", // 开始时间
                endDates: "", // 截至时间
                modelType: "0", // 0待办 1已办 2查询
                userId: "", // 查询待办或已办时传 列表页查询不传
                page: 1, // 当前页码
                pageSize: 20, // 每页显示条数

            },
            queryCriteriaData: {}, //分页
            table_config: formJs.table_config, //列表
            dataFid: null, //列表详情id
            tableData: [],

            // 弹框
            isShow: false, //是否显示弹框
            statusNum: null, //点击那个按钮
            currentRow: {}, //列表详情
            dialogTitle: "",
            // 弹框
            userInfo: {}, //登录用户

        };
    },
    components: {
        TableLyout,
        CustomDialog,//详情
        TableData, //主页面列表
    },
    created() { },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        console.log("userInfo", this.userInfo);
        this.getList = {
            // projectId: this.userInfo.projectId,// 项目ID
            fCompanyid: this.userInfo.companyId, // 左侧选中组织机构id
            startDates: "", // 开始时间
            endDates: "", // 截至时间
            modelType: "0", // 0待办 1已办 2查询
            userId: this.userInfo.userId, // 查询待办或已办时传 列表页查询不传
            page: 1, // 当前页码
            pageSize: 20, // 每页显示条数
        };

        this.yj_searchForm(this.getList);
        this.btnRole(this.name);
        this.table_config.operation.cols = [
            {
                prop: "fStatus",
                type: "primary",
                handleRow: this.handleRow,
            },
        ];

        this.table_config.operation["specialClass"] = function (rowData) {
            var color = null;
            if (rowData == "待检测") {
                color = "red !important";
            } else if (rowData == "检测中") {
                color = "orange !important";
            } else if (rowData == "已完成") {
                color = "green !important";
            } else {
                color = "#409eff !important";
            }
            return color;
        };
    },
    methods: {
        // 弹框
        refreshList() { //刷新列表
            this.$nextTick(() => {
                this.yj_searchForm(this.getList);
            })
        },
        closeDialog() {
            this.isShow = false;
        },
        // 弹框


        // 待办/已办
        modelType(e) {
            this.currentRow = "";
            this.dataFid = "";
            if (e == "待办") {
                this.getList.modelType = "0";
            } else {
                this.getList.modelType = "1";
            }
            this.yj_searchForm(this.getList);
        },

        // 列表操作按钮
        handleRow() { },

        // 获取列表数据
        yj_searchForm(id) {
            this.loading = true;
            getSteelProtectionTestTaskLists(id)
                .then((res) => {
                    if (res.errcode == 0) {
                        var that = this;
                        that.loading = false;
                        that.tableData = res.data.pageInfo.list;
                        that.queryCriteriaData = res.data.pageInfo;
                    } else {
                        this.$message.error("列表", res.msg);
                        this.loading = false;
                    }
                })
                .catch(() => {
                    this.loading = false;
                    console.log("获取列表数据接口报错");
                });
        },


        // 导航栏-右侧按钮
        btnRole(name) {
            if (this.userInfo.userId != 'System') {
                var btnList = [];
                var ModuleList = JSON.parse(sessionStorage.getItem("yjSmart-ModuleList"));
                if (ModuleList.length > 0) {
                    ModuleList.map((item) => {
                        if (item.fEncode == name) {
                            btnList = item.modulebuttonList;
                        }
                    });
                    btnList.map(item => {
                        item['disabled'] = false
                    })
                    this.btnObj = btnList
                }
            } else {
                this.btnObj = formJs.btnObj
            }
        },
        getBtnName(name, status) {
            this.statusNum = status;
            this.yj_edit(status)
        },
        // 导航栏---右侧按钮



        // 新增/编辑
        yj_edit(num) {
            if (this.userInfo.companyNature == "总监办" || this.userInfo.companyNature == "施工单位"|| this.userInfo.companyNature == "中心试验室") {
                if (num == 1) { // 新增
                    this.dialogTitle = "新增";
                    this.editText();
                } else if (num == 2) { //复制
                    this.yj_copy(this.currentRow);
                } else if (num == 3) {//编辑
                    this.dialogTitle = "编辑";
                    if (this.dataFid != null) {
                        if (this.currentRow.fStatus == "编辑中" || this.currentRow.fStatus == "被撤回") {
                            this.editText();
                        } else {
                            this.$message.warning("请选择信息状态为'编辑中'或'被撤回'的数据！");
                        }
                    } else {
                        this.$message.warning("请先选择需要编辑的数据");
                    }
                } else if (num == 4) { //提交
                    if (this.dataFid != null) {
                        if (this.currentRow.fStatus == "编辑中" || this.currentRow.fStatus == "被撤回") {
                            if (this.currentRow.fEngineeringpart != "" && this.currentRow.fEngineeringpart != null) {
                                this.yj_submit(this.currentRow);
                            } else {
                                this.$message.warning("请先填写工程部位！");
                            }
                        } else {
                            this.$message.warning("请选择信息状态为'编辑中'或'被撤回'的数据！");
                        }
                    } else {
                        this.$message.warning("请先选择需要提交的数据");
                    }

                } else if (num == 5) { //删除
                    this.yj_delete(this.currentRow);
                }
            } else {
                this.$message.warning("该用户无法操作");
            }
        },
        // 编辑
        editText() {
            this.isShow = true;
        },
        // 复制
        yj_copy(row) {
            if (row.fId != "" && row.fId != null && row.fId != undefined) {
                if (row.fStatus == "编辑中") {
                    this.$confirm("是否继续复制?", "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    })
                        .then(() => {
                            this.loading = true;
                            copySteelProtectionTestTask({
                                fId: row.fId,
                                userId: this.userInfo.userId,
                                userName: this.userInfo.realName,
                            })
                                .then((res) => {
                                    if (res.errcode == 0) {
                                        this.loading = false;
                                        this.yj_searchForm(this.getList);
                                        this.$message.success("复制成功");
                                    } else {
                                        this.$message.error("复制失败", res.msg);
                                        this.loading = false;
                                    }
                                })
                                .catch(() => {
                                    console.log("复制失败");
                                    this.loading = false;
                                });
                        })
                        .catch(() => {
                            this.$message("取消复制");
                        });
                } else {
                    return this.$message.warning('请选择信息状态为"编辑中"的数据！');
                }
            } else {
                return this.$message.warning("请先选择需要复制的数据");
            }
        },
        // 提交
        yj_submit() {
            if (this.currentRow != null) {
                if (this.dataFid != "" && this.dataFid != null) {
                    this.$confirm("是否继续提交?", "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    })
                        .then(() => {
                            this.isLock = true
                            this.loading = true;
                            submitSteelProtectionTestTaskByAuthor({
                                fId: this.dataFid,
                                // userId: this.userInfo.userId, //当前登录人id
                                // userName: this.userInfo.realName //当前登录人姓名
                            })
                                .then((res) => {
                                    if (res.errcode == 0) {
                                        this.loading = false;
                                        this.dataFid = null
                                        this.isLock = false
                                        this.yj_searchForm(this.getList);
                                        this.$message.success("提交成功");
                                    } else {
                                        this.isLock = false
                                        this.$message.error("提交失败", res.msg);
                                        this.loading = false;
                                        return false;
                                    }
                                })
                                .catch(() => {
                                    this.loading = false;
                                    console.log("提交失败");
                                });
                        })
                        .catch(() => {
                            this.$message("取消提交");
                        });
                } else {
                    return this.$message.error("请先选择需要提交的数据");
                }
            }
        },
        // 删除
        yj_delete(row) {
            if (row.fId != "" && row.fId != null && row.fId != undefined) {
                if (row.fStatus == "编辑中"||row.fStatus == "被撤回中") {
                    this.$confirm("是否继续删除?", "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    })
                        .then(() => {
                            this.loading = true;
                            this.isLock = true
                            deleteSteelProtectionTestTask({
                                fId: row.fId,
                            })
                                .then((res) => {
                                    if (res.errcode == 0) {
                                        this.isLock = false;
                                        this.loading = false;
                                        this.yj_searchForm(this.getList);
                                        this.$message.success("删除成功");
                                    } else {
                                        this.isLock = false;
                                        this.$message.error("删除失败", res.msg);
                                        this.loading = false;
                                    }
                                })
                                .catch(() => {
                                    console.log("删除失败");
                                    this.loading = false;
                                });
                        })
                        .catch(() => {
                            this.$message("取消删除");
                        });
                } else {
                    return this.$message.warning('请选择信息状态为"编辑中"的数据！');
                }
            } else {
                return this.$message.warning("请先选择需要删除的数据");
            }
        },

        // 点击查看
        yj_viewForm(row, label) {
            // console.log("yj_viewForm",row, label);
            this.currentRow = row;
            if (label == "工程部位") {
                this.statusNum = 7;
                this.isShow = true;
                this.dialogTitle = "查看";
            }
        },
        // 点击当前行变色
        rowChange(val) {
            this.currentRow = val
            this.dataFid = val.fId;
        },
    },
};